package com.whfc.wxmp.api.base.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.R;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.emp.dto.AppCorpGroupDTO;
import com.whfc.emp.dto.AppEmpDTO;
import com.whfc.emp.dto.AppGroupDTO;
import com.whfc.emp.dto.AppWorkTypeDTO;
import com.whfc.fuum.dto.AppCorpDTO;
import com.whfc.fuum.dto.SysDeptDTO;
import com.whfc.fuum.dto.SysMachListDTO;
import com.whfc.fuum.dto.SysWorkAreaDTO;
import com.whfc.fuum.entity.WxUser;
import com.whfc.wxmp.api.base.service.WxOptionService;
import com.whfc.wxmp.api.emp.service.WxAppEmpWorkService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/8/1 9:36
 */
@RestController
@RequestMapping("/mp/api/option")
public class WxOptionController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxOptionService optionService;

    @Autowired
    private WxAppEmpWorkService wxAppEmpWorkService;

    /**
     * 获取当前用户可以查看的项目列表
     *
     * @param user
     * @return
     */
    @GetMapping("project/list")
    public Result getProjectList(@CurrentWxUser WxUser user) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取项目列表", RequestConetxtUtil.getParameters());
        List<SysDeptDTO> deptDTOList = optionService.getProjectList(user);
        ListData data = new ListData(deptDTOList);
        return ResultUtil.success(data);
    }

    /**
     * 获取当前用户可以查看的组织机构
     *
     * @param user   当前登录用户
     * @param deptId 所选项目ID
     * @return
     */
    @GetMapping("dept/list")
    public Result getDeptList(@CurrentWxUser WxUser user,
                              @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取组织机构列表", "{\"deptId\":\"" + deptId + "\"}");
        List<SysDeptDTO> deptDTOList = optionService.getDeptList(user, deptId);
        ListData data = new ListData(deptDTOList);
        return ResultUtil.success(data);
    }

    /**
     * 获取设备类型/品牌/型号
     *
     * @param user
     * @param hotFlag
     * @return
     */
    @Deprecated
    @GetMapping("mach/type/list")
    public Result getMachTypeList(@CurrentWxUser WxUser user,
                                  @RequestParam("hotFlag") Integer hotFlag) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询所有设备类型列表", "{\"hotFlag\":\"" + hotFlag + "\"}");
        return ResultUtil.success();
    }

    /**
     * 2.0 根据组织机构获取合作单位
     *
     * @param user
     * @param deptId
     * @return
     */
    @GetMapping("corp/list")
    public Result corpList(@CurrentWxUser WxUser user,
                           @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取合作单位", "{\"deptId\":\"" + deptId + "\"}");
        ListData<AppCorpDTO> listData = optionService.corpList(deptId);
        return ResultUtil.success(listData);
    }

    @GetMapping("/group/list")
    public Result groupList(@CurrentWxUser WxUser user,
                            @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取班组列表", RequestConetxtUtil.getParameters());
        ListData<AppGroupDTO> listData = optionService.groupList(deptId);
        return ResultUtil.success(listData);
    }

    @GetMapping("/emp/list")
    public Result getEmpList(@CurrentWxUser WxUser user,
                             @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取可供选择的人员列表", RequestConetxtUtil.getParameters());
        ListData<AppEmpDTO> listData = optionService.getEmpList(deptId);
        return ResultUtil.success(listData);
    }

    @GetMapping("/machTypeList")
    public Result machTypeList(@CurrentWxUser WxUser user,@RequestParam("machTypeId") Integer machTypeId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取可供选择的设备类型列表", RequestConetxtUtil.getParameters());
        ListData<SysMachListDTO> listData = optionService.machTypeList(machTypeId);
        return ResultUtil.success(listData);
    }

    @GetMapping(value = "/workType/list")
    public Result workTypeList(@CurrentWxUser WxUser user,
                               @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取可供选择的工种列表(不分页)", RequestConetxtUtil.getParameters());
        ListData<AppWorkTypeDTO> listData = wxAppEmpWorkService.workTypeList(deptId);
        return ResultUtil.success(listData);
    }

    @GetMapping(value = "/work/area/list")
    public Result workAreaList(@CurrentWxUser WxUser user,
                               @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取工区列表(不分页)", RequestConetxtUtil.getParameters());
        ListData<SysWorkAreaDTO> listData = optionService.getWorkAreaList(deptId);
        return ResultUtil.success(listData);
    }


    /**
     * 2.0 获取合作单位以及下属班组列表
     * @param user
     * @param deptId
     * @return
     */
    @GetMapping("/corp/group/list")
    public R<List<AppCorpGroupDTO>> corpGroupList(@CurrentWxUser WxUser user,
                            @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取合作单位和班组列表", RequestConetxtUtil.getParameters());
        List<AppCorpGroupDTO> listData = optionService.corpGroupList(deptId);
        return R.success(listData);
    }
}
